package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateCustomProcessBaseEnvironmentRel
//@description: 创建CustomProcessBaseEnvironmentRel记录
//@param: customProcessBaseEnvironmentRel model.CustomProcessBaseEnvironmentRel
//@return: err error

func CreateCustomProcessBaseEnvironmentRel(customProcessBaseEnvironmentRel model.CustomProcessBaseEnvironmentRel) (err error) {
	err = global.GVA_DB.Create(&customProcessBaseEnvironmentRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseEnvironmentRel
//@description: 删除CustomProcessBaseEnvironmentRel记录
//@param: customProcessBaseEnvironmentRel model.CustomProcessBaseEnvironmentRel
//@return: err error

func DeleteCustomProcessBaseEnvironmentRel(customProcessBaseEnvironmentRel model.CustomProcessBaseEnvironmentRel) (err error) {
	err = global.GVA_DB.Delete(customProcessBaseEnvironmentRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseEnvironmentRelByIds
//@description: 批量删除CustomProcessBaseEnvironmentRel记录
//@param: ids request.IdsReq
//@return: err error

func DeleteCustomProcessBaseEnvironmentRelByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.CustomProcessBaseEnvironmentRel{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCustomProcessBaseEnvironmentRel
//@description: 更新CustomProcessBaseEnvironmentRel记录
//@param: customProcessBaseEnvironmentRel *model.CustomProcessBaseEnvironmentRel
//@return: err error

func UpdateCustomProcessBaseEnvironmentRel(customProcessBaseEnvironmentRel model.CustomProcessBaseEnvironmentRel) (err error) {
	err = global.GVA_DB.Save(&customProcessBaseEnvironmentRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseEnvironmentRel
//@description: 根据id获取CustomProcessBaseEnvironmentRel记录
//@param: id uint
//@return: err error, customProcessBaseEnvironmentRel model.CustomProcessBaseEnvironmentRel

func GetCustomProcessBaseEnvironmentRel(id uint) (err error, customProcessBaseEnvironmentRel model.CustomProcessBaseEnvironmentRel) {
	err = global.GVA_DB.Where("id = ?", id).First(&customProcessBaseEnvironmentRel).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseEnvironmentRelInfoList
//@description: 分页获取CustomProcessBaseEnvironmentRel记录
//@param: info request.CustomProcessBaseEnvironmentRelSearch
//@return: err error, list interface{}, total int64

func GetCustomProcessBaseEnvironmentRelInfoList(info request.CustomProcessBaseEnvironmentRelSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.CustomProcessBaseEnvironmentRel{})
    var customProcessBaseEnvironmentRels []model.CustomProcessBaseEnvironmentRel
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.EnvironmentId != 0 {
        db = db.Where("`environment_id` = ?",info.EnvironmentId)
    }
    if info.BaseId != 0 {
        db = db.Where("`base_id` = ?",info.BaseId)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&customProcessBaseEnvironmentRels).Error
	return err, customProcessBaseEnvironmentRels, total
}